package com.usian.controller;

import com.usian.ContentFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/backend/content")
public class ContentController {

    @Autowired
    private ContentFeign contentFeign;

        /**
         * 根据节点id查询商品内容信息
         *
         * @param id
         * @return
         */
    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Long id){
        List<TbContentCategory> contentCategoryList= contentFeign.selectContentCategoryByParentId(id);
        if(contentCategoryList.size()>0){
            return Result.ok(contentCategoryList);
        }
        return  Result.error("查无数据");
    }

    /**
     * 添加内容节点
     *
     * @param parentId
     * @param name
     * @return
     */
    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(Long parentId,String name ){
        Integer num= contentFeign.insertContentCategory(parentId,name);
        if(num>0){
            return Result.ok("");
        }
        return  Result.error("保存失败");
    }


    /**
     * 删除节点
     *
     * @param categoryId
     * @return
     */
    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(Long categoryId){
        Integer num= contentFeign.deleteContentCategoryById(categoryId);
        if(num == 200){
            return Result.ok("");
        }
        return  Result.error("删除失败");
    }


    /**
     *
     * 分页查询商品内容
     *
     * @param categoryId
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam Long categoryId,
                                                 @RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "10") Integer rows){
        PageResult pageResult = contentFeign.selectTbContentAllByCategoryId(categoryId,page,rows);
        if(pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return  Result.error("查询失败");
    }

    /**
     * 保存商品内容信息
     *
     * @param tbContent
     * @return
     */
    @RequestMapping("insertTbContent")
    public Result insertTbContent(TbContent tbContent){
        Integer num = contentFeign.insertTbContent(tbContent);
        if(num>0){
            return Result.ok("");
        }
        return  Result.error("保存失败");
    }

    /**
     * 删除商品内容信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("deleteContentByIds")
    public Result deleteContentByIds(Long ids){
        Integer num = contentFeign.deleteContentByIds(ids);
        if(num>0){
            return Result.ok("");
        }
        return  Result.error("删除失败");
    }

}
